﻿namespace Hourglass.Qrobot.Utils
{
    public static class HashHelper
    {
        public static string QqHash(long qq, string ptwebqq)
        {
            var t = new int[4];
            for (var i = 0; i < ptwebqq.Length; i++)
            {
                t[i % 4] ^= ptwebqq[i];
            }
            var flags = new[] {"EC", "OK"};
            var t1 = new long[4];
            t1[0] = qq >> 24 & 255 ^ flags[0][0];
            t1[1] = qq >> 16 & 255 ^ flags[0][1];
            t1[2] = qq >> 8 & 255 ^ flags[1][0];
            t1[3] = qq & 255 ^ flags[1][1];

            var t2 = new long[8];

            for (var i = 0; i < 8; i++)
            {
                t2[i] = i % 2 == 0 ? t[i >> 1] : t1[i >> 1];
            }

            var hexItems = new[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
            var hash = "";

            foreach (long tt in t2)
            {
                hash += hexItems[(int)((tt >> 4) & 15)];
                hash += hexItems[(int)(tt & 15)];
            }
            return hash;
        }
    }
}
